package com.eightworld3.supermarket.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eightworld3.supermarket.domain.WareInfo;
import com.eightworld3.supermarket.domain.WareType;
import com.eightworld3.supermarket.mapper.WareInfoMapper;
import com.eightworld3.supermarket.mapper.WareTypeMapper;
import com.eightworld3.supermarket.service.AssetsEnum;
import com.eightworld3.supermarket.service.WareInfoService;
import com.eightworld3.supermarket.service.dto.WareInfoDTO;
import com.eightworld3.supermarket.service.dto.add.WareInfoAddDTO;
import com.eightworld3.supermarket.service.dto.result.ResponseResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author EightWorld3
 */
@Service
public class WareInfoServiceImpl extends ServiceImpl<WareInfoMapper,WareInfo> implements WareInfoService {
    private final WareInfoMapper wareInfoMapper;
    private final WareTypeMapper wareTypeMapper;

    /**
     * WareInfoServiceImpl 构造方法
     * @param wareInfoMapper 商品信息Mapper
     * @param wareTypeMapper 商品类型Mapper
     */
    public WareInfoServiceImpl(WareInfoMapper wareInfoMapper, WareTypeMapper wareTypeMapper){
        this.wareInfoMapper = wareInfoMapper;
        this.wareTypeMapper = wareTypeMapper;
    }

    /**
     * 查询所有
     * @return 返回所有WareInfo(商品信息)
     */
    @Override
    public List<WareInfo> selectAll(){
        return wareInfoMapper.selectList(null);
    }

    /**
     * 根据wareId查询WareInfo
     * @param wareId ID号
     * @return 返回wareId对应的WareInfo对象(经ResponseResult包装类修饰)
     */
    @Override
    public ResponseResult selectByWareId(Integer wareId){
        if (wareId == null) {
            return new ResponseResult(2, "null");
        }

        WareInfo entity = wareInfoMapper.selectById(wareId);
        if (entity == null) {
            return new ResponseResult(2, "没有该对象");
        }

        return new ResponseResult(1,"成功",entity);
    }

    /**
     * 新增WareInfo
     * @param wareInfoAddDTO WareInfo新增DTO对象
     * @return 返回当前当次新增状况(经ResponseResult包装类修饰)
     */
    @Override
    public ResponseResult addWareInfo(WareInfoAddDTO wareInfoAddDTO){
        WareInfo entity = new WareInfo();

        // 商品类型存在判断
        WareType wareTypeEntity = wareTypeMapper.selectById(wareInfoAddDTO.getWareTypeId());
        if (wareTypeEntity == null){
            return new ResponseResult(2, "商品类型选择错误");
        }else {
            entity.setWareTypeId(wareInfoAddDTO.getWareTypeId());
        }

        // 商品名称字符大小限制
        if(wareInfoAddDTO.getWareName().getBytes().length > AssetsEnum.NAME_VARCHAR_LIMIT.value()){
            return new ResponseResult(2, "商品名称过长，字符大小需 <= 32Byte");
        }else {
            entity.setWareName(wareInfoAddDTO.getWareName());
        }

        // 商品等级字符大小限制
        if(wareInfoAddDTO.getWareLevel().getBytes().length > AssetsEnum.WARE_LEVEL_VARCHAR_LIMIT.value()){
            return new ResponseResult(2, "商品等级过长，字符大小需 <= 4Byte");
        }else {
            entity.setWareLevel(wareInfoAddDTO.getWareLevel());
        }

        // 商品总数大于零判定
        if(wareInfoAddDTO.getWareTotalNumber() < AssetsEnum.BOUNDARY_VALUE_ZERO.value()){
            return new ResponseResult(2, "商品总数需 >=0");
        }else {
            entity.setWareTotalNumber(wareInfoAddDTO.getWareTotalNumber());
        }

        // 上下架限制
        if (wareInfoAddDTO.getWareUpAndDown() < 0 || wareInfoAddDTO.getWareUpAndDown() > 1){
            return new ResponseResult(2, "上下架状态有误");
        }else {
            entity.setWareUpAndDown(wareInfoAddDTO.getWareUpAndDown());
        }

        // 备注内容字符限制
        if(wareInfoAddDTO.getWareRemark().getBytes().length > AssetsEnum.REMARK_VARCHAR_LIMIT.value()){
            return new ResponseResult(2, "备注内容过多，需 <= 120Byte");
        }else {
            entity.setWareRemark(wareInfoAddDTO.getWareRemark());
        }

        return new ResponseResult(1,"成功",wareInfoMapper.insert(entity));
    }

    /**
     * 更新WareInfo
     * @param wareInfoDTO WareInfo DTO对象
     * @return 返回当前当次更新状况(经ResponseResult包装类修饰)
     */
    @Override
    public ResponseResult updateWareInfo(WareInfoDTO wareInfoDTO){
        WareInfo entity = new WareInfo();

        // 修改对象存在判断
        WareInfo existEntity = wareInfoMapper.selectById(wareInfoDTO.getWareId());
        if(existEntity == null){
            return new ResponseResult(2, "商品价格对象不存在");
        }else {
            entity.setWareId(wareInfoDTO.getWareId());
        }

        // 商品类型存在判断
        WareType wareTypeEntity = wareTypeMapper.selectById(wareInfoDTO.getWareTypeId());
        if (wareTypeEntity == null){
            return new ResponseResult(2, "商品类型选择错误");
        }else {
            entity.setWareTypeId(wareInfoDTO.getWareTypeId());
        }

        // 商品名称字符大小限制
        if(wareInfoDTO.getWareName().getBytes().length > AssetsEnum.NAME_VARCHAR_LIMIT.value()){
            return new ResponseResult(2, "商品名称过长，字符大小需 <= 32Byte");
        }else {
            entity.setWareName(wareInfoDTO.getWareName());
        }

        // 商品等级字符大小限制
        if(wareInfoDTO.getWareLevel().getBytes().length > AssetsEnum.WARE_LEVEL_VARCHAR_LIMIT.value()){
            return new ResponseResult(2, "商品等级过长，字符大小范围 <= 4Byte");
        }else {
            entity.setWareLevel(wareInfoDTO.getWareLevel());
        }

        // 商品总数大于零判定
        if(wareInfoDTO.getWareTotalNumber() <= AssetsEnum.BOUNDARY_VALUE_ZERO.value()){
            return new ResponseResult(2, "商品总数需大于0");
        }else {
            entity.setWareTotalNumber(wareInfoDTO.getWareTotalNumber());
        }

        // 上下架限制
        if (wareInfoDTO.getWareUpAndDown() < 0 || wareInfoDTO.getWareUpAndDown() > 1){
            return new ResponseResult(2, "上下架状态有误");
        }else {
            entity.setWareUpAndDown(wareInfoDTO.getWareUpAndDown());
        }

        // 备注内容字符限制
        if(wareInfoDTO.getWareRemark().getBytes().length > AssetsEnum.REMARK_VARCHAR_LIMIT.value()){
            return new ResponseResult(2, "备注内容过多，需 <= 120Byte");
        }else {
            entity.setWareRemark(wareInfoDTO.getWareRemark());
        }

        return new ResponseResult(1,"成功",wareInfoMapper.updateById(entity));
    }

    /**
     * 删除WareInfo
     * @param wareId ID号
     * @return 返回当前当次删除状况(经ResponseResult包装类修饰)
     */
    @Override
    public ResponseResult deleteWareInfoByWareId(int wareId){
        int deleteNum = wareInfoMapper.deleteById(wareId);
        if (deleteNum == 0) {
            return new ResponseResult(0, "对象不存在");
        }
        return new ResponseResult(1, "成功");
    }
}
